package com.lmb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmb.bean.Employee;
import com.lmb.bean.R;
import com.lmb.exception.CustomException;
import com.lmb.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RequestMapping("/employee")
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     * @param session
     * @param emp
     * @return
     */
    @PostMapping("/login")
    public R login(HttpSession session, @RequestBody Employee emp){
        log.info("【登录员工信息：{}】",emp);
        String password = DigestUtils.md5DigestAsHex(emp.getPassword().getBytes());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,emp.getUsername());
        Employee employee = employeeService.getOne(wrapper);
        if (employee==null){
            return R.error("员工不存在！");
        }
        if (password==null){
            return R.error("员工账号和密码不匹配！");
        }
        if (employee.getStatus()==0){
            return R.error("员工已禁用！");
        }
        session.setAttribute("employee",employee.getId());
        return R.success(employee);
    }

    //退出登录
    @PostMapping("/logout")
    public R logout(HttpSession  session){
        session.invalidate();
        //2.返回处理结果
        return R.success("退出成功！");
    }

    //新增员工
    @PostMapping
    public R add(HttpSession session,@RequestBody Employee emp){
        emp.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        emp.setStatus(1);

        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        Long employeeId = (Long) session.getAttribute("employee");
        emp.setCreateUser(employeeId);
        emp.setUpdateUser(employeeId);

        boolean flag = employeeService.save(emp);
        return flag?R.success(true):R.error("新增员工失败！");
    }

    /**
     * 条件分页查询
     * @param page     当前页
     * @param pageSize 当前页展示N条数据
     * @param name     条件查询参数
     * @return
     */
    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,String name){
        System.out.println(page + "   " + pageSize + "  "+name);
        Page<Employee> pages = new Page<>(page, pageSize);

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(null != name,Employee::getName, name);

        employeeService.page(pages, wrapper);

        return R.success(pages);
    }

    /**
     * 根据Id查询单个用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getEmpById(@PathVariable String id){
        System.out.println(id);
        Employee employee = employeeService.getById(id);
        return  R.success(employee);
    }

    @PutMapping
    public R upEmployee(@RequestBody Employee employee,HttpSession session){
        System.out.println(employee);
        Long upDateUser = (Long) session.getAttribute("employee");
        employee.setUpdateUser(upDateUser);
        employee.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null != employee.getUsername(),Employee::getUsername,employee.getUsername());
        wrapper.ne(null != employee.getId(),Employee::getId,employee.getId());
        int count = employeeService.count(wrapper);
        if ( employee.getUsername() !=null && count>0) {
            throw new CustomException("员工用户名["+employee.getUsername()+"]已存在!!!!!");
        }
        boolean b = employeeService.updateById(employee);

        return b?R.success("修改成功"):R.error("修改失败");
    }



}
